package nuist.hanniko.stu.webbackend.dao.mapper;

import nuist.hanniko.stu.webbackend.model.LwM2m.LwM2mResource;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface LwM2mResourceMapper {

    @Insert({
            "insert ignore into device_resources(device_id, object_id, resource_id, name, ops, type) ",
            "values (#{deviceId}, #{objectId}, #{resourceId}, #{name}, #{operations}, #{type})"
    })
    void insert(LwM2mResource resource);

    @Insert({
            "<script>",
            "insert ignore into device_resources(device_id, object_id, resource_id, name, ops, type) ",
            "values ",
            "<foreach collection='resources' item='item' separator=','>",
            "( #{item.deviceId}, #{item.objectId}, #{item.resourceId}, #{item.name}, #{item.operations}, #{item.type})",
            "</foreach>",
            "</script>"
    })
    void insertBatch(@Param("resources") List<LwM2mResource> resources);

    @Select("select * from device_resources where device_id=#{deviceId}")
    List<LwM2mResource> getResourceInfoByDeviceId(String deviceId);

    @Select("select * from device_resources where device_id=#{deviceId} and object_id=#{objectId}")
    List<LwM2mResource> getResourceInfoByDeviceIdAndObjectId(String deviceId, String objectId);

    @Select("select * from device_resources where device_id=#{deviceId} and object_id=#{objectId} and resource_id=#{resourceId}")
    LwM2mResource getResourceInfo(String deviceId, String objectId, String resourceId);
}
